package com.lanou3g.dllo.baidumusic.login;

import android.content.Intent;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.BaseActivity;
import com.lanou3g.dllo.baidumusic.main.MainActivity;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by dllo on 16/7/6.
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    private ImageView registerReturn;
    private EditText registerUsername, registerPassword;
    private Button registerBtn;
    private CheckBox registerCheckBox;

    @Override
    public int setLayout() {
        return R.layout.activity_register;
    }

    @Override
    public void initView() {
        registerReturn = (ImageView) findViewById(R.id.register_return);
        registerUsername = (EditText) findViewById(R.id.register_username);
        registerPassword = (EditText) findViewById(R.id.register_password);
        registerBtn = (Button) findViewById(R.id.register_btn);
        registerCheckBox = (CheckBox) findViewById(R.id.register_checkbox);

        registerReturn.setOnClickListener(this);
        registerBtn.setOnClickListener(this);
        registerCheckBox.setOnClickListener(this);

    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register_return:
                finish();
                break;
            case R.id.register_btn:
                String inputInfo = registerUsername.getText().toString();
                String inputinfo = registerPassword.getText().toString();
                if (inputInfo.isEmpty() || inputinfo.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "请输入用户名或密码", Toast.LENGTH_SHORT).show();
                } else {
                    BmobUser bmobUser = new BmobUser();
                    bmobUser.setUsername(registerUsername.getText().toString());
                    bmobUser.setPassword(registerPassword.getText().toString());
                    bmobUser.signUp(this, new SaveListener() {
                        @Override
                        public void onSuccess() {
                            Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                        @Override
                        public void onFailure(int i, String s) {
                            Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                break;
            case R.id.register_checkbox:
              if (registerCheckBox.isChecked()) {
                  registerPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
              } else {
                  registerPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
              }
                break;
        }

    }
}
